package com.example.demo.topic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.topic.entity.TNoteComment;
import com.example.demo.topic.service.TNoteCommentService;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 发帖评论表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/tNoteComment")
@Tag(name = "tNoteComment", description = "发帖评论表")
public class TNoteCommentController {
    @Autowired
    private TNoteCommentService tNoteCommentService;

    /**
     * 获取发帖评论模块列表
     */
    @GetMapping("/getNoteList")
    @Operation(summary = "获取发帖评论列表接口", description = "获取发帖评论列表接口")
    public Result<?> getNoteList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                 @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                 TNoteComment note){
        Integer uid = note.getUid();
        note.setUid(null);
        Page<TNoteComment> page = new Page<>(current,size);
        QueryWrapper<TNoteComment> queryWrapper = new QueryWrapper<>(note);
        queryWrapper.select("t_note_comment.*,(select count(*) from t_comment_likes where t_comment_likes.cid = t_note_comment.id) as likes," +
                "(select avatar from u_user where u_user.id = t_note_comment.uid) as avatar,(select name from u_user where u_user.id = t_note_comment.uid) as name," +
                "(select EXISTS(SELECT 1 from t_comment_likes where t_comment_likes.cid = t_note_comment.id and t_comment_likes.uid = "+uid+")) as izLike");
        queryWrapper.orderByDesc("likes","created_time");
        return Result.ok(tNoteCommentService.page(page,queryWrapper));
    }

    /**
     * 根据ID查询发帖评论信息
     */
    @GetMapping("/getNoteById")
    @Operation(summary = "根据id查询发帖评论明细", description = "根据id查询发帖评论明细")
    public Result<?> getNoteById(Integer id){
        return Result.ok(tNoteCommentService.getById(id));
    }

    /**
     * 修改发帖评论信息
     */
    @PutMapping("/updateNote")
    @Operation(summary = "根据id修改发帖评论明细", description = "根据id修改发帖评论明细")
    @Transactional
    public Result<?> updateNote(@RequestBody TNoteComment note){
        return Result.ok(tNoteCommentService.updateById(note));
    }

    /**
     * 新增发帖评论信息
     */
    @PostMapping("/addNote")
    @Operation(summary = "新增发帖评论明细", description = "新增发帖评论明细")
    public Result<?> addNote(@RequestBody TNoteComment note){
        return Result.ok(tNoteCommentService.save(note));
    }

    /**
     * 删除发帖评论信息
     */
    @DeleteMapping("/deleteNoteById")
    @Operation(summary = "删除发帖评论", description = "删除发帖评论")
    @Transactional
    public Result<?> deleteNoteById(Integer id){
        return Result.ok(tNoteCommentService.removeById(id));
    }
}

